package com.prolestar.notice.service.impl;

import com.prolestar.Enum.PsNoticeFeedbackType;
import com.prolestar.Enum.PsNoticeRecordStatus;
import com.prolestar.Enum.PsNoticeSendUnits;
import com.prolestar.Enum.PsNoticeStatus;
import com.prolestar.common.core.domain.AjaxResult;
import com.prolestar.common.core.domain.entity.SysDept;
import com.prolestar.common.core.domain.model.LoginUser;
import com.prolestar.common.utils.DateUtils;
import com.prolestar.common.utils.ServletUtils;
import com.prolestar.notice.domain.PsNotice;
import com.prolestar.notice.domain.PsNoticeCondition;
import com.prolestar.notice.domain.PsNoticeDTO;
import com.prolestar.notice.domain.PsNoticeRecord;
import com.prolestar.notice.mapper.PsNoticeMapper;
import com.prolestar.notice.service.IPsNoticeService;
import com.prolestar.service.TokenServices;
import com.prolestar.system.service.ISysDeptService;
import com.prolestar.system.service.ISysRoleService;
import com.prolestar.system.service.ISysUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 通知公告Service业务层处理
 * 
 * @author xuyp
 * @date 2022-11-12
 */
@Service
public class PsNoticeServiceImpl implements IPsNoticeService 
{
    @Autowired
    private PsNoticeMapper psNoticeMapper;

    @Autowired
    private TokenServices tokenServices;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysDeptService sysDeptService;



    /**
     * 查询通知公告
     * 
     * @param id 通知公告主键
     * @return 通知公告
     */
    @Override
    public PsNotice selectPsNoticeById(Long id)
    {
        return psNoticeMapper.selectPsNoticeById(id);
    }

    /**
     * 查询通知公告列表
     * 
     * @param psNotice 通知公告
     * @return 通知公告
     */
    @Override
    public List<PsNotice> selectPsNoticeList(PsNotice psNotice)
    {
        LoginUser loginUser = tokenServices.getLoginUser(ServletUtils.getRequest());
        psNotice.setDeptId(loginUser.getDeptId());
        Set<String> permission = sysRoleService.selectRolePermissionByUserId(loginUser.getUserId());
        if (permission.contains("admin")||permission.contains("conservator")){
            List<PsNotice> psNotices = psNoticeMapper.selectPsNoticeList(psNotice);
            psNotices.forEach(ps->{
                String userName = sysUserService.selectUserById(Long.parseLong(ps.getCreateBy())).getUserName();
                ps.setCreateBy(userName);
            });
            return psNotices;
        }else {
            List<PsNotice> psNotices = psNoticeMapper.selectPsNoticeUserList(psNotice);
            psNotices.forEach(ps->{
                String userName = sysUserService.selectUserById(Long.parseLong(ps.getCreateBy())).getUserName();
                ps.setCreateBy(userName);
            });
            return psNotices;
        }
    }

    /**
     * 通知公告--新建
     * @param
     * @return
     */
    @Override
    public AjaxResult apportion(PsNoticeDTO psNoticeDTO) {
        LoginUser loginUser = tokenServices.getLoginUser(ServletUtils.getRequest());
        psNoticeDTO.setCreateBy(loginUser.getUserId());
        psNoticeDTO.setCreateTime(DateUtils.getNowDate());
        if (psNoticeDTO.getStatus()==PsNoticeStatus.MANUSCRIPT){
            psNoticeDTO.setStatus(PsNoticeStatus.MANUSCRIPT);
             psNoticeMapper.insertPsNotice(psNoticeDTO);
        }else if (psNoticeDTO.getStatus()==PsNoticeStatus.SEND && Long.parseLong(psNoticeDTO.getSendUnits()) == PsNoticeSendUnits.SENDUNITS){
            psNoticeDTO.setStatus(PsNoticeStatus.SEND);
            psNoticeMapper.insertApportionPsNotice(psNoticeDTO);
            List<Long> unitIds = psNoticeDTO.getUnitIds();
            unitIds.forEach(unitId -> {
                psNoticeDTO.setNoticeId(psNoticeDTO.getId());
                psNoticeDTO.setUnitId(unitId.toString());
                psNoticeDTO.setStatus(PsNoticeRecordStatus.NOREADRECEIPT);
                psNoticeMapper.insertPsNoticeRecord(psNoticeDTO);
            });
        }else if (psNoticeDTO.getStatus()==PsNoticeStatus.SEND && Long.parseLong(psNoticeDTO.getSendUnits()) == PsNoticeSendUnits.ALLSENDUNITS){  // 判断是否发送全部
            List<SysDept> sysDepts = sysDeptService.selectunitIds();
            psNoticeDTO.setStatus(PsNoticeStatus.SEND);
            psNoticeMapper.insertApportionPsNotice(psNoticeDTO);
            sysDepts.forEach(dept ->{
                Long deptId = dept.getDeptId();
                psNoticeDTO.setNoticeId(psNoticeDTO.getId());
                psNoticeDTO.setUnitId(deptId.toString());
                psNoticeDTO.setStatus(PsNoticeRecordStatus.NOREADRECEIPT);
                psNoticeMapper.insertPsNoticeRecord(psNoticeDTO);
            });
        }
        return AjaxResult.success();
    }



    /**
     * 修改通知公告
     * 
     * @param psNotice 通知公告
     * @return 结果
     */
    @Override
    public int updatePsNotice(PsNotice psNotice)
    {
        return psNoticeMapper.updatePsNotice(psNotice);
    }

    /**
     * 批量删除通知公告
     * 
     * @param ids 需要删除的通知公告主键
     * @return 结果
     */
    @Override
    public int deletePsNoticeByIds(Long[] ids)
    {
        return psNoticeMapper.deletePsNoticeByIds(ids);
    }

    /**
     * 删除通知公告信息
     * 
     * @param id 通知公告主键
     * @return 结果
     */
    @Override
    public int deletePsNoticeById(Long id)
    {
        return psNoticeMapper.deletePsNoticeById(id);
    }


    /**
     * 通知公告--处理反馈
     * @param psNoticeRecord
     * @return
     */
    @Override
    public AjaxResult dispose(PsNoticeRecord psNoticeRecord) {
        LoginUser loginUser = tokenServices.getLoginUser(ServletUtils.getRequest());
        psNoticeRecord.setStatus(PsNoticeRecordStatus.RECEIVEDFEEDBACK);
        psNoticeRecord.setFeedbackBy(loginUser.getUserId());
        psNoticeRecord.setFeedbackTime(DateUtils.getNowDate());
        psNoticeRecord.setUnitId(loginUser.getDeptId().toString());
        psNoticeMapper.updateDisposePsNoticeRecord(psNoticeRecord);
        return AjaxResult.success();
    }

    /**
     * 撤回
     * @param
     * @return
     */
    @Override
    public AjaxResult withdraw(PsNotice psNotice) {
        psNotice.setStatus(PsNoticeStatus.EXPURGATE);
        psNoticeMapper.updatePsNotice(psNotice);
        return AjaxResult.success();
    }

    /**
     * 处置情况
     * @param psNoticeRecord
     * @return
     */
    @Override
    public List<PsNoticeRecord> dispositionList(PsNoticeRecord psNoticeRecord) {
        List<PsNoticeRecord> psNoticeRecords = psNoticeMapper.selectDispositionList(psNoticeRecord);
        ArrayList<PsNoticeRecord> noticeRecords = new ArrayList<>();
        psNoticeRecords.forEach(ps ->{
            PsNoticeRecord psNoticeRecord1 = new PsNoticeRecord();
            String unitName = null;
            String nickName = null;
            if (ps.getUnitId() != null){
                unitName = sysDeptService.selectDeptById(Long.parseLong(ps.getUnitId())).getDeptName();
            }
            if (ps.getFeedbackBy() != null){
                nickName = sysUserService.selectUserById(ps.getFeedbackBy()).getNickName();
            }
            psNoticeRecord1.setUnitName(unitName);
            psNoticeRecord1.setFeedbackByName(nickName);
            psNoticeRecord1.setStatus(ps.getStatus());
            psNoticeRecord1.setFeedbackContent(ps.getFeedbackContent());
            noticeRecords.add(psNoticeRecord1);
        });
        return noticeRecords;
    }

    /**
     * 已读
     * @param psNoticeRecord
     * @return
     */
    @Override
    public AjaxResult yesRead(PsNoticeRecord psNoticeRecord) {
        LoginUser loginUser = tokenServices.getLoginUser(ServletUtils.getRequest());
        psNoticeRecord.setStatus(PsNoticeRecordStatus.READRECEIPT);
        psNoticeRecord.setUnitId(loginUser.getDeptId().toString());
         psNoticeMapper.updateYesRead(psNoticeRecord);
         return AjaxResult.success();
    }

    /**
     * 接受情况
     * @param
     * @return
     */
    @Override
    public PsNoticeCondition acceptance(PsNotice psNotice) {
        if (psNotice.getFeedbackType() == PsNoticeFeedbackType.ACCEPTANCENUMBER){
            PsNoticeCondition psNoticeCondition = new PsNoticeCondition();
             Integer acceptanceNumber = psNoticeMapper.readStatus(psNotice);
             Integer transmissionsNumber = psNoticeMapper.selectTransmissionsNumber(psNotice);
             List<PsNoticeRecord> psNoticeRecords = psNoticeMapper.selectPsNotice(psNotice);
             psNoticeCondition.setAcceptanceNumber(acceptanceNumber);
             psNoticeCondition.setTransmissionsNumber(transmissionsNumber);
             psNoticeCondition.setPsNoticeRecords(psNoticeRecords);
             return psNoticeCondition;
        }else {
            PsNoticeCondition psNoticeCondition = new PsNoticeCondition();
            Integer acceptanceNumber = psNoticeMapper.feedbackStatus(psNotice);
            Integer transmissionsNumber = psNoticeMapper.selectTransmissionsNumber1(psNotice);
            List<PsNoticeRecord> psNoticeRecords = psNoticeMapper.selectPsNotice1(psNotice);
            psNoticeCondition.setAcceptanceNumber(acceptanceNumber);
            psNoticeCondition.setTransmissionsNumber(transmissionsNumber);
            psNoticeCondition.setPsNoticeRecords(psNoticeRecords);
            return psNoticeCondition;
        }
    }


}
